//
//  StartPageView.m
//  carcareIOS
//
//  Created by wr on 16/3/17.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "StartPageView.h"
#import "AIUserInfo.h"
#import "UserHelper.h"
#import "ScreenMgr.h"

@interface StartPageView()

@property (nonatomic , strong) NSTimer * timer;

@property (nonatomic , assign) NSInteger timeNum;

@property (nonatomic , strong) UILabel * timeLabel;

@property (nonatomic , strong) UIImageView * adverticeImageV;

@end
@implementation StartPageView
-(void)dealloc{

}
- (instancetype)init
{
    self = [super initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT)];
    if (self) {
        self.backgroundColor = COLOR_HEXSTRING(@"F6F6F6");
        
        self.timeNum = 1;
        
        [Factory imageViewWithFrame:CGRectMake(0, (__MAIN_WIDTH == 320 ? -(568 - __MAIN_HEIGHT) / 2 : 0), __MAIN_WIDTH, __MAIN_HEIGHT) image:ImageNamed(@"StartPageBackImage") onView:self];
        
        [self createUI];
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimePage) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
    return self;
}
- (void)setModel:(AdverticePageModel *)model
{
    _model = model;
    self.timeNum = 5;
    if ([model.modelDic count] > 0) {
        
        [self.timeLabel setHidden:NO];
        [self.adverticeImageV setHidden:NO];
        if([model.img isNotNil])[self.adverticeImageV setImageWithURL:[NSURL URLWithString:model.img]];
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture)];
        [self.adverticeImageV addGestureRecognizer:tapGesture];
    }
}
- (void)runTimePage
{
    self.timeNum --;
    if (self.timeNum == 0) {
        [self removeSelfFromWindow];
    }else
    {
        self.timeLabel.text = [NSString stringWithFormat:@"%zd 秒",self.timeNum];
    }
    
}
- (void)createUI
{
    CGFloat footerViewH = 135 * __MAIN_RATIO_375;
    UIView * footerView =[[[UIView viewWithFrame:CGRectMake(0, self.height - footerViewH, self.width, footerViewH)] resetBackgroundColor:[UIColor clearColor]] setupOnView:self];
    
//    UIImage * startImage = ImageNamed(@"StatPageTextImage");
//    [Factory imageViewWithCenter:CGPointMake(footerView.width / 2, footerView.height / 2) image:startImage onView:footerView];
    
    self.adverticeImageV = [Factory imageViewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH,footerView.top) image:nil onView:self];
    self.adverticeImageV.backgroundColor = [UIColor clearColor];
    self.adverticeImageV.userInteractionEnabled = YES;
    
    
    CGFloat labelX = 22 * __MAIN_RATIO_375;
    CGFloat buttonW = 50;
    self.timeLabel = [Factory labelWithFrame:CGRectMake(self.adverticeImageV.width - buttonW - labelX , 32 * __MAIN_RATIO_375, buttonW, 20) font:FONT_DEFAULT_Light(12) text:[NSString stringWithFormat:@"%zd 秒",self.timeNum] textColor:[Theme mainNavColor] onView:self.adverticeImageV textAlignment:NSTextAlignmentCenter];
    
    [self.timeLabel setHidden:YES];
    [self.adverticeImageV setHidden:YES];
    
    
    __weak __typeof(self) wself = self;
    UIButton * exitBtn = [[[UIButton buttonWithFrame:CGRectMake(self.adverticeImageV.width - buttonW - labelX, self.timeLabel.bottom + 10 * __MAIN_RATIO_375, buttonW, 35) title:@"跳过" textColor:[UIColor whiteColor] font:FONT_BUTTON click:^{
        [wself removeSelfFromWindow];
    }] resetBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.6]] setupOnView:self.adverticeImageV];

    exitBtn.layer.cornerRadius = 2;
    exitBtn.layer.masksToBounds = YES;
}
- (void)tapGesture
{
    [self removeSelfFromWindow];
    if (self.adverticeClick) self.adverticeClick(self.model);
}
- (void)removeSelfFromWindow
{
    [self.timer invalidate];
    self.timer = nil;
    [self removeFromSuperview];
    [SCREEN showRightScreen];

}
@end
